package com.tcm.day03;

import java.util.ArrayList;
import java.util.List;

public class Code01_linkedList {

    static List<Integer> v1 = new ArrayList();
    static List<Integer> v2 = new ArrayList();

    public static void main(String[] args) {
//        Node head = new Node(1);
//        head.next = new Node(2);
//        head.next.next = new Node(3);
//        f(head);
//        System.out.println(head.val);

        System.out.println("测试开始");
        Node head = randomDoubleNode(20, 20);
//        Node reverseHead = reverseLinkedList(head);
        Node reverseHead = reverseDoubleLinkedList(head);
        if (!test(head, reverseHead)) {
            System.err.println("出错了");
        }
        System.out.println("测试结束");
    }

    public static boolean test(Node head, Node reverseHead) {

        List<Integer> list2 = new ArrayList<>();
        while (reverseHead != null) {
            list2.add(reverseHead.val);
            reverseHead = reverseHead.next;
        }

        int idx = 0;
        for (int i = v1.size() - 1; i >= 0; i--) {
            if (v1.get(i) != list2.get(idx)) {
                return false;
            }
            idx++;
        }
        return true;
    }

    public static void f(Node h) {
        h = h.next;
    }

    public static Node randomNode(int len, int maxVal) {
        int nodeLen = (int) (Math.random() * len) + 1;
        Node head = null;
        Node cur = null;
        v1.clear();
        for (int i = 0; i < nodeLen; i++) {
            if (null == cur) {
                cur = new Node((int) (Math.random() * maxVal));
                v1.add(cur.val);
            } else {
                cur.next = new Node((int) (Math.random() * maxVal));
                v1.add(cur.next.val);
            }
            if (null == head) {
                head = cur;
            }
            while (cur != null && cur.next != null) {
                cur = cur.next;
            }
        }
        return head;
    }

    public static Node randomDoubleNode(int len, int maxVal) {
        int nodeLen = (int) (Math.random() * len) + 1;
        Node head = null;
        Node cur = null;
        v1.clear();
        for (int i = 0; i < nodeLen; i++) {
            if (null == cur) {
                cur = new Node((int) (Math.random() * maxVal));
                v1.add(cur.val);
            } else {
                cur.next = new Node((int) (Math.random() * maxVal));
                cur.next.prev = cur;
                v1.add(cur.next.val);
            }
            if (null == head) {
                head = cur;
            }
            while (cur != null && cur.next != null) {
                cur = cur.next;
            }
        }
        return head;
    }

    public static Node reverseLinkedList(Node head) {
        Node prev = null;
        Node cur = head;
        Node next = null;

        while (cur != null) {
            next = cur.next;
            cur.next = prev;
            prev = cur;
            cur = next;
        }
        return prev;
    }

    public static Node reverseDoubleLinkedList(Node head) {
        Node prev = null;
        Node cur = head;
        Node next = null;

        while (cur != null) {
            next = cur.next;
            cur.next = prev;
            cur.prev = next;
            prev = cur;
            cur = next;
        }
        return prev;
    }

}

